<template>
	<view class="searchLayout">
		<view class="search">
			<uni-search-bar focus placeholder="搜索" v-model="queryParams.keyword" @clear="onClear" @cancel="onClear" @confirm="onSearch"></uni-search-bar>
		</view>
		<view class="history" v-if="historySearch.length">
			<view class="topTitle">
				<view class="text">最近搜索</view>
				<uni-icons type="trash" size="25" @click="removeHistory"></uni-icons>
			</view>

			<view class="tabs">
				<view class="tab" v-for="tab in historySearch" :key="tab" @click="clickTab(tab)">{{ tab }}</view>
			</view>
		</view>

		<view class="recommend">
			<view class="topTitle">
				<view class="text">热门搜索</view>
			</view>
			<view class="tabs">
				<view class="tab" v-for="tab in hotSearch" :key="tab" @click="clickTab(tab)">{{ tab }}</view>
			</view>
		</view>

		<view class="noSearch" v-show="!classList.length">
			<uv-empty mode="search" icon="https://cdn.uviewui.com/uview/empty/search.png"></uv-empty>
		</view>

		<view>
			<view class="list">
				<navigator :url="`/pages/preview/preview?id=${item._id}`" class="item" v-for="item in classList" :key="item._id">
					<image :src="item.smallPicurl" mode="aspectFill"></image>
				</navigator>
			</view>
			<!-- 列表有数据的时候显示 -->
			<view class="loadingLayout" v-if="noMoreData || classList.length">
				<uni-load-more :status="noMoreData?'noMore':'loading'"></uni-load-more>
			</view>
		</view>
		
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { apiSearchWall } from '@/api/apis.js';
import { onReachBottom } from '@dcloudio/uni-app'

onLoad(() => {
	//获取搜索记录的缓存数据
	historySearch.value = uni.getStorageSync('historySearch') || [];
});

const classList = ref([]);

//搜索记录
const historySearch = ref([]);
//热门搜索
const hotSearch = ref(['美女', '帅哥', '宠物', '卡通']);

//没有搜索结果
const noSearchData = ref(false)

//没有更多数据
const noMoreData = ref(false)

//查询参数
const queryParams = ref({
	keyword: '',
	pageNum: 1,
	pageSize: 12
});

//清空输入的内容，取消搜索
const onClear = () => {
	console.log('onClear');
	initParams();
};

//点击标签进行搜索
const clickTab = (value) => {
	initParams(value);
	onSearch();
};

const initParams = (value) => {
	classList.value = [];
	noSearchData.value = false;
	noMoreData.value = false;
	queryParams.value = {
		pageNum: 1,
		pageSize: 12,
		keyword: value || ''
	};
};

//点击搜索
const onSearch = () => {
	console.log(queryParams.value.keyword);
	uni.showLoading();
	//将 输入关键字 添加到 historySearch.value 数组中，并去除重复元素，然后取前10个元素
	historySearch.value = [...new Set([queryParams.value.keyword, ...historySearch.value])].slice(0, 10);
	//将记录保存到本地缓存
	uni.setStorageSync('historySearch', historySearch.value);
	//初始化参数
	initParams(queryParams.value.keyword);
	//请求数据
	searchData();
};

//请求网络数据
const searchData = async () => {
	try {
		let res = await apiSearchWall(queryParams.value);
		console.log(res.data);
		//拼接数组
		classList.value = [...classList.value, ...res.data];
		//将列表数据保存到缓存中，在详情页面滑动查看
		uni.setStorageSync('storageClassList', classList.value);
		//判断是否还有最后一页数据
		if(queryParams.value.pageSize > res.data.length ) noMoreData.value = true
		//是否空数据
		if(res.data.length == 0 && classList.value.length == 0) noSearchData.value = true
	} finally {
		uni.hideLoading();
	}
};

onReachBottom(()=>{
	if(noMoreData.value) return
	queryParams.value.pageNum ++;
	searchData()
})


//清除历史记录
const removeHistory = () => {
	uni.showModal({
		title: '是否清除历史搜索',
		success: (res) => {
			if (res.confirm) {
				uni.removeStorageSync('historySearch');
				historySearch.value = [];
			}
		}
	});
};
</script>

<style lang="scss" scoped>
.searchLayout {
	.search {
		padding: 0 10rpx;
	}
	.history {
		padding: 30rpx;
	}
	.topTitle {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 32rpx;
		color: #999;
	}

	.recommend {
		padding: 30rpx;
	}
	.tabs {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		padding-top: 20rpx;
		.tab {
			background: #f4f4f4;
			font-size: 28rpx;
			color: #333;
			border-radius: 50rpx;
			margin-top: 20rpx;
			margin-left: 20rpx;
			padding: 10rpx 28rpx;
		}
	}
	.list {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 5rpx;
		padding: 20rpx 5rpx;
		.item {
			height: 440rpx;
			image {
				width: 100%;
				height: 100%;
				display: block;
			}
		}
	}
}
</style>
